package edu.itla.medifacil.persistence.managers;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import edu.itla.medifacil.persistence.entities.People;
import edu.itla.medifacil.persistence.enums.Role;

public class PatientManager {
	private EntityManager em = null;
	
	public PatientManager() {
		this.em = Persistence.createEntityManagerFactory("MediFacil").createEntityManager();
	}
	

	public People save( Map<String, String> parameters ){
		PersonManager patientManager = new PersonManager();
		
		return patientManager.save(parameters, Role.PATIENT.getId());
	}
	
	
	public void delete( int patientId ){
		PersonManager patientManager = new PersonManager();
		patientManager.delete(patientId);
	}

	public People find( int patientId ){
		em.clear();
		return em.find( People.class, patientId);
	}
	
	@SuppressWarnings("unchecked")
	public List<People> findAll(){
		return (List<People>) em.createQuery( "SELECT p FROM People p WHERE p.user.role.id =" + edu.itla.medifacil.persistence.enums.Role.PATIENT.getId() ).getResultList();
	}
}
